/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managedBean;

import bean.business.subjectBean;
import entities.Subject;
import java.util.LinkedList;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

/**
 *
 * @author HoangHai
 */
@ManagedBean
@SessionScoped
public class manageSubject {
    @EJB
    private subjectBean subjectBean;
    private List<Subject> list;
    private Subject sub = new Subject();
    private Subject sub1 = new Subject();

    public Subject getSub1() {
        return sub1;
    }

    public void setSub1(Subject sub1) {
        this.sub1 = sub1;
    }

    public List<Subject> getList() {
        list = subjectBean.allSubject();
        return list;
    }

    public void setList(List<Subject> list) {
        this.list = list;
    }

    public Subject getSub() {
        return sub;
    }

    public void setSub(Subject sub) {
        this.sub = sub;
    }
    
    /** Creates a new instance of manageSubject */
    public manageSubject() {
    }
    
    public void insert(){
        if (checkName()) {
            sub1.setPassMark(sub1.getScaleMark()*0.4);
            subjectBean.addSubject(sub1);
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Sucessful", "Sucessful"));
        }
    }
    public void update(){
        sub.setPassMark(sub.getScaleMark()*0.4);
        subjectBean.updateSubject(sub);
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Sucessful", "Sucessful"));
    }
    public void delete(){
        subjectBean.delSubject(sub.getSubID());
    }
    public boolean checkName(){
        List<Subject> lsub = new LinkedList<Subject>();
        lsub = subjectBean.allSubject();
        for (Subject subject : lsub) {
            if (subject.getSubName().equals(sub1.getSubName())) {
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error", "Name had been Already"));
                return false;
            }
        }
        return true;
    }
}
